package cn.miaopasi.demo.service.impl;

import cn.miaopasi.demo.mapper.ExerciseMapper;
import cn.miaopasi.demo.model.Exercise;
import cn.miaopasi.demo.service.ExerciseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 活动管理默认实现
 *
 * @author lixingwu
 * @since 2021-10-20
 */
@Service
public class ExerciseServiceImpl extends ServiceImpl<ExerciseMapper, Exercise> implements ExerciseService {
    @Override
    public Long add(Exercise entity) {
        // 1.校验数据
        if ("".equals(entity.getExerciseTitle()) || (null == entity.getExerciseTitle())) {
            throw new RuntimeException("活动标题必须填写");
        }
        // 2.添加记录
        if (!save(entity)) {
            throw new RuntimeException("活动信息保存失败");
        }
        return entity.getId();
    }

    @Override
    public Exercise update(Exercise entity) {
        // 1.校验数据
        if (null == entity.getId()) {
            throw new RuntimeException("更新的记录ID必须传入");
        }
        // 2.保持失败直接抛出异常
        if (!updateById(entity)) {
            throw new RuntimeException("活动信息更新失败");
        }
        return getById(entity.getId());
    }

    @Override
    public Exercise find(Exercise entity) {
        LambdaQueryWrapper<Exercise> queryWrapper = new QueryWrapper<Exercise>()
                .setEntity(entity)
                .lambda();
        return getOne(queryWrapper);
    }

    @Override
    public List<Exercise> list(Exercise entity) {
        LambdaQueryWrapper<Exercise> queryWrapper = new QueryWrapper<Exercise>()
                .setEntity(entity)
                .lambda();
        return list(queryWrapper);
    }

    @Override
    public Page<Exercise> page(Exercise entity, int page, int limit) {
        Page<Exercise> exercisePage = new Page<>(page, limit);
        LambdaQueryWrapper<Exercise> queryWrapper = new QueryWrapper<Exercise>()
                .setEntity(entity)
                .lambda();
        return page(exercisePage, queryWrapper);
    }
}
